package com.hpsk.shopservice.model.config.controller;

import cn.hutool.json.JSONUtil;
import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.MapSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.operator.Contain;
import cn.zhxu.bs.util.MapUtils;
import com.hpsk.common.CommonException;
import com.hpsk.common.RespResult;
import com.hpsk.common.RespUtils;
import com.hpsk.shopsatoken.model.user.entity.User;
import com.hpsk.shopservice.model.config.dto.TestDto;
import com.hpsk.shopservice.model.config.event.TestApplicationEvent;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author huangwang2832511019
 */
@RestController
@RequestMapping("/test/api")
@Slf4j
@Tag(name = "测试类")
@Validated
public class TestController {
    /**
     * 注入 Map 检索器，它检索出来的数据以 Map 对象呈现
     */
    @Autowired
    private MapSearcher mapSearcher;
    /**
     * 注入 Bean 检索器，它检索出来的数据以 泛型 对象呈现
     */
    @Autowired
    private BeanSearcher beanSearcher;

    @Resource
    private ApplicationContext applicationContext;

    @RequestMapping(value = "/testBeanSearcher",method = RequestMethod.GET)
    @Operation(summary = " BeanSearcher Bean 检索器，它检索出来的数据以 泛型 对象呈现")
    public RespResult<List<User>> testBeanSearcher() {
        SearchResult<User> search = beanSearcher.search(User.class);
        List<User> dataList = search.getDataList();
        log.info("dataListBean:{}", dataList);
        return RespUtils.ok(dataList);
    }

    @RequestMapping(value = "/testMapSearcher",method = RequestMethod.GET)
    @Operation(summary = " BeanSearcher Map 检索器，它检索出来的数据以 Map 对象呈现")
    public RespResult<List<Map<String, Object>>> testMapSearcher() {
        SearchResult<Map<String, Object>> search = mapSearcher.search(User.class);
        List<Map<String, Object>> dataList = search.getDataList();
        log.info("dataListMap:{}", dataList);
        return RespUtils.ok(dataList);
    }

    @RequestMapping(value = "/beanSearcher",method = RequestMethod.GET)
    @Operation(summary = " BeanSearcher 条件查询")
    public RespResult<List<Map<String, Object>>> beanSearcher(@ParameterObject @Valid TestDto test) {
        Map<String, Object> build = MapUtils.builder().field(User::getName, test.getName()).op(Contain.class).build();
        SearchResult<Map<String, Object>> search = mapSearcher.search(User.class,build);
        List<Map<String, Object>> dataList = search.getDataList();
        log.info("dataListMap:{}", dataList);
        return RespUtils.ok(dataList);
    }

    @RequestMapping(value = "/testApplicationListener",method = RequestMethod.GET)
    @Operation(summary = " BeanSearcher 条件查询")
    public RespResult<String> testApplicationListener() {
        Map<String, Object> build = MapUtils.builder().build();
        List<Map<String, Object>> maps = mapSearcher.searchList(User.class, build);
        TestApplicationEvent<List<Map<String, Object>>> listTestApplicationEvent = new TestApplicationEvent<>(this, maps);
        List<Map<String, Object>> object = listTestApplicationEvent.getObject();
        log.info("object:{}", object);
        applicationContext.publishEvent(listTestApplicationEvent);
        return RespUtils.ok();
    }


    @RequestMapping(value = "/t",method = RequestMethod.GET)
    @Operation(summary = "t查询")
    public String test() {
        log.info("test");
        return "test";
    }

    @RequestMapping(value = "/err",method = RequestMethod.GET)
    @Operation(summary = "t查询")
    public RespResult<String> testErr() {
        log.info("test");
        try {
            int i = Integer.parseInt("wwwww");
        } catch (NumberFormatException e) {
            throw new CommonException(e.getMessage());
        }
        return RespUtils.ok();
    }
    @PostMapping("/add")
    public RespResult<String> add(@RequestBody @Valid TestDto test) {
        log.info("入参:【{}】", JSONUtil.toJsonStr(test));
        return RespUtils.ok("添加成功");
    }
}
